home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2005 March / Macworld CD March 2005 - Marathon Trilogy.iso / Shareware World / iPod / iPodderX.sit / iPodderX / iPodderX.app / Contents / Resources / Encrypter.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2005-01-07  |  30.9 KB  |  842 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.3)
  3.  
  4. from cStringIO import StringIO
  5. from binascii import b2a_hex
  6. from socket import error as socketerror
  7. protocol_name = 'BitTorrent protocol'
  8.  
  9. def toint(s):
  10.     return long(b2a_hex(s), 16)
  11.  
  12.  
  13. def tobinary(i):
  14.     return chr(i >> 24) + chr(i >> 16 & 255) + chr(i >> 8 & 255) + chr(i & 255)
  15.  
  16.  
  17. class Connection:
  18.     
  19.     def __init__(self, Encoder, connection, id, is_local):
  20.         self.encoder = Encoder
  21.         self.connection = connection
  22.         self.id = id
  23.         self.locally_initiated = is_local
  24.         self.complete = False
  25.         self.closed = False
  26.         self.buffer = StringIO()
  27.         self.next_len = 1
  28.         self.next_func = self.read_header_len
  29.         if self.locally_initiated:
  30.             connection.write(chr(len(protocol_name)) + protocol_name + chr(0) * 8 + self.encoder.download_id)
  31.             if self.id is not None:
  32.                 connection.write(self.encoder.my_id)
  33.             
  34.         
  35.  
  36.     
  37.     def get_ip(self):
  38.         return self.connection.get_ip()
  39.  
  40.     
  41.     def get_id(self):
  42.         return self.id
  43.  
  44.     
  45.     def is_locally_initiated(self):
  46.         return self.locally_initiated
  47.  
  48.     
  49.     def is_flushed(self):
  50.         return self.connection.is_flushed()
  51.  
  52.     
  53.     def read_header_len(self, s):
  54.         if ord(s) != len(protocol_name):
  55.             return None
  56.         
  57.         return (len(protocol_name), self.read_header)
  58.  
  59.     
  60.     def read_header(self, s):
  61.         if s != protocol_name:
  62.             return None
  63.         
  64.         return (8, self.read_reserved)
  65.  
  66.     
  67.     def read_reserved(self, s):
  68.         return (20, self.read_download_id)
  69.  
  70.     
  71.     def read_download_id(self, s):
  72.         if s != self.encoder.download_id:
  73.             return None
  74.         
  75.         if not (self.locally_initiated):
  76.             self.connection.write(chr(len(protocol_name)) + protocol_name + chr(0) * 8 + self.encoder.download_id + self.encoder.my_id)
  77.         
  78.         return (20, self.read_peer_id)
  79.  
  80.     
  81.     def read_peer_id(self, s):
  82.         if not (self.id):
  83.             if s == self.encoder.my_id:
  84.                 return None
  85.             
  86.             for v in self.encoder.connections.values():
  87.                 if s and v.id == s:
  88.                     return None
  89.                     continue
  90.             
  91.             self.id = s
  92.             if self.locally_initiated:
  93.                 self.connection.write(self.encoder.my_id)
  94.             else:
  95.                 self.encoder.everinc = True
  96.         elif s != self.id:
  97.             return None
  98.         
  99.         self.complete = True
  100.         self.encoder.connecter.connection_made(self)
  101.         return (4, self.read_len)
  102.  
  103.     
  104.     def read_len(self, s):
  105.         l = toint(s)
  106.         if l > self.encoder.max_len:
  107.             return None
  108.         
  109.         return (l, self.read_message)
  110.  
  111.     
  112.     def read_message(self, s):
  113.         if s != '':
  114.             self.encoder.connecter.got_message(self, s)
  115.         
  116.         return (4, self.read_len)
  117.  
  118.     
  119.     def read_dead(self, s):
  120.         return None
  121.  
  122.     
  123.     def close(self):
  124.         if not (self.closed):
  125.             self.connection.close()
  126.             self.sever()
  127.         
  128.  
  129.     
  130.     def sever(self):
  131.         self.closed = True
  132.         del self.encoder.connections[self.connection]
  133.         if self.complete:
  134.             self.encoder.connecter.connection_lost(self)
  135.         
  136.  
  137.     
  138.     def send_message(self, message):
  139.         self.connection.write(tobinary(len(message)) + message)
  140.  
  141.     
  142.     def data_came_in(self, s):
  143.         while True:
  144.             if self.closed:
  145.                 return None
  146.             
  147.             i = self.next_len - self.buffer.tell()
  148.             if i > len(s):
  149.                 self.buffer.write(s)
  150.                 return None
  151.             
  152.             self.buffer.write(s[:i])
  153.             s = s[i:]
  154.             m = self.buffer.getvalue()
  155.             self.buffer.reset()
  156.             self.buffer.truncate()
  157.             
  158.             try:
  159.                 x = self.next_func(m)
  160.             except:
  161.                 (self.next_len, self.next_func) = (1, self.read_dead)
  162.                 raise 
  163.  
  164.             if x is None:
  165.                 self.close()
  166.                 return None
  167.             
  168.             (self.next_len, self.next_func) = x
  169.  
  170.  
  171.  
  172. class Encoder:
  173.     
  174.     def __init__(self, connecter, raw_server, my_id, max_len, schedulefunc, keepalive_delay, download_id, max_initiate = 40):
  175.         self.raw_server = raw_server
  176.         self.connecter = connecter
  177.         self.my_id = my_id
  178.         self.max_len = max_len
  179.         self.schedulefunc = schedulefunc
  180.         self.keepalive_delay = keepalive_delay
  181.         self.download_id = download_id
  182.         self.max_initiate = max_initiate
  183.         self.everinc = False
  184.         self.connections = { }
  185.         self.spares = []
  186.         schedulefunc(self.send_keepalives, keepalive_delay)
  187.  
  188.     
  189.     def send_keepalives(self):
  190.         self.schedulefunc(self.send_keepalives, self.keepalive_delay)
  191.         for c in self.connections.values():
  192.             if c.complete:
  193.                 c.send_message('')
  194.                 continue
  195.         
  196.  
  197.     
  198.     def start_connection(self, dns, id):
  199.         if id:
  200.             if id == self.my_id:
  201.                 return None
  202.             
  203.             for v in self.connections.values():
  204.                 if v.id == id:
  205.                     return None
  206.                     continue
  207.             
  208.         
  209.         if len(self.connections) >= self.max_initiate:
  210.             if len(self.spares) < self.max_initiate and dns not in self.spares:
  211.                 self.spares.append(dns)
  212.             
  213.             return None
  214.         
  215.         
  216.         try:
  217.             c = self.raw_server.start_connection(dns)
  218.             self.connections[c] = Connection(self, c, id, True)
  219.         except socketerror:
  220.             pass
  221.  
  222.  
  223.     
  224.     def _start_connection(self, dns, id):
  225.         
  226.         def foo(self = self, dns = dns, id = id):
  227.             self.start_connection(dns, id)
  228.  
  229.         self.schedulefunc(foo, 0)
  230.  
  231.     
  232.     def got_id(self, connection):
  233.         for v in self.connections.values():
  234.             if connection is not v and connection.id == v.id:
  235.                 connection.close()
  236.                 return None
  237.                 continue
  238.         
  239.         self.connecter.connection_made(connection)
  240.  
  241.     
  242.     def ever_got_incoming(self):
  243.         return self.everinc
  244.  
  245.     
  246.     def external_connection_made(self, connection):
  247.         self.connections[connection] = Connection(self, connection, None, False)
  248.  
  249.     
  250.     def connection_flushed(self, connection):
  251.         c = self.connections[connection]
  252.         if c.complete:
  253.             self.connecter.connection_flushed(c)
  254.         
  255.  
  256.     
  257.     def connection_lost(self, connection):
  258.         self.connections[connection].sever()
  259.         while len(self.connections) < self.max_initiate and self.spares:
  260.             self.start_connection(self.spares.pop(), None)
  261.  
  262.     
  263.     def data_came_in(self, connection, data):
  264.         self.connections[connection].data_came_in(data)
  265.  
  266.  
  267.  
  268. class DummyConnecter:
  269.     
  270.     def __init__(self):
  271.         self.log = []
  272.         self.close_next = False
  273.  
  274.     
  275.     def connection_made(self, connection):
  276.         self.log.append(('made', connection))
  277.  
  278.     
  279.     def connection_lost(self, connection):
  280.         self.log.append(('lost', connection))
  281.  
  282.     
  283.     def connection_flushed(self, connection):
  284.         self.log.append(('flushed', connection))
  285.  
  286.     
  287.     def got_message(self, connection, message):
  288.         self.log.append(('got', connection, message))
  289.         if self.close_next:
  290.             connection.close()
  291.         
  292.  
  293.  
  294.  
  295. class DummyRawServer:
  296.     
  297.     def __init__(self):
  298.         self.connects = []
  299.  
  300.     
  301.     def start_connection(self, dns):
  302.         c = DummyRawConnection()
  303.         self.connects.append((dns, c))
  304.         return c
  305.  
  306.  
  307.  
  308. class DummyRawConnection:
  309.     
  310.     def __init__(self):
  311.         self.closed = False
  312.         self.data = []
  313.         self.flushed = True
  314.  
  315.     
  316.     def get_ip(self):
  317.         return 'fake.ip'
  318.  
  319.     
  320.     def is_flushed(self):
  321.         return self.flushed
  322.  
  323.     
  324.     def write(self, data):
  325.         if not not (self.closed):
  326.             raise AssertionError
  327.         self.data.append(data)
  328.  
  329.     
  330.     def close(self):
  331.         if not not (self.closed):
  332.             raise AssertionError
  333.         self.closed = True
  334.  
  335.     
  336.     def pop(self):
  337.         r = ''.join(self.data)
  338.         del self.data[:]
  339.         return r
  340.  
  341.  
  342.  
  343. def dummyschedule(a, b):
  344.     pass
  345.  
  346.  
  347. def test_messages_in_and_out():
  348.     c = DummyConnecter()
  349.     rs = DummyRawServer()
  350.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  351.     c1 = DummyRawConnection()
  352.     e.external_connection_made(c1)
  353.     if not c1.pop() == '':
  354.         raise AssertionError
  355.     if not c.log == []:
  356.         raise AssertionError
  357.     if not rs.connects == []:
  358.         raise AssertionError
  359.     if not not (c1.closed):
  360.         raise AssertionError
  361.     e.data_came_in(c1, chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20)
  362.     if not c1.pop() == chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'a' * 20:
  363.         raise AssertionError
  364.     if not c.log == []:
  365.         raise AssertionError
  366.     if not rs.connects == []:
  367.         raise AssertionError
  368.     if not not (c1.closed):
  369.         raise AssertionError
  370.     e.data_came_in(c1, 'b' * 20)
  371.     if not c1.pop() == '':
  372.         raise AssertionError
  373.     if not len(c.log) == 1:
  374.         raise AssertionError
  375.     if not c.log[0][0] == 'made':
  376.         raise AssertionError
  377.     ch = c.log[0][1]
  378.     del c.log[:]
  379.     if not rs.connects == []:
  380.         raise AssertionError
  381.     if not not (c1.closed):
  382.         raise AssertionError
  383.     if not ch.get_ip() == 'fake.ip':
  384.         raise AssertionError
  385.     ch.send_message('abc')
  386.     if not c1.pop() == chr(0) * 3 + chr(3) + 'abc':
  387.         raise AssertionError
  388.     if not c.log == []:
  389.         raise AssertionError
  390.     if not rs.connects == []:
  391.         raise AssertionError
  392.     if not not (c1.closed):
  393.         raise AssertionError
  394.     e.data_came_in(c1, chr(0) * 3 + chr(3) + 'def')
  395.     if not c1.pop() == '':
  396.         raise AssertionError
  397.     if not c.log == [
  398.         ('got', ch, 'def')]:
  399.         raise AssertionError
  400.     del c.log[:]
  401.     if not rs.connects == []:
  402.         raise AssertionError
  403.     if not not (c1.closed):
  404.         raise AssertionError
  405.  
  406.  
  407. def test_flushed():
  408.     c = DummyConnecter()
  409.     rs = DummyRawServer()
  410.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  411.     c1 = DummyRawConnection()
  412.     e.external_connection_made(c1)
  413.     if not c1.pop() == '':
  414.         raise AssertionError
  415.     if not c.log == []:
  416.         raise AssertionError
  417.     if not rs.connects == []:
  418.         raise AssertionError
  419.     if not not (c1.closed):
  420.         raise AssertionError
  421.     e.data_came_in(c1, chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20)
  422.     if not c1.pop() == chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'a' * 20:
  423.         raise AssertionError
  424.     if not c1.pop() == '':
  425.         raise AssertionError
  426.     if not c.log == []:
  427.         raise AssertionError
  428.     if not rs.connects == []:
  429.         raise AssertionError
  430.     if not not (c1.closed):
  431.         raise AssertionError
  432.     e.connection_flushed(c1)
  433.     if not c1.pop() == '':
  434.         raise AssertionError
  435.     if not c.log == []:
  436.         raise AssertionError
  437.     if not rs.connects == []:
  438.         raise AssertionError
  439.     if not not (c1.closed):
  440.         raise AssertionError
  441.     e.data_came_in(c1, 'b' * 20)
  442.     if not c1.pop() == '':
  443.         raise AssertionError
  444.     if not len(c.log) == 1:
  445.         raise AssertionError
  446.     if not c.log[0][0] == 'made':
  447.         raise AssertionError
  448.     ch = c.log[0][1]
  449.     del c.log[:]
  450.     if not rs.connects == []:
  451.         raise AssertionError
  452.     if not not (c1.closed):
  453.         raise AssertionError
  454.     if not ch.is_flushed():
  455.         raise AssertionError
  456.     e.connection_flushed(c1)
  457.     if not c1.pop() == '':
  458.         raise AssertionError
  459.     if not c.log == [
  460.         ('flushed', ch)]:
  461.         raise AssertionError
  462.     if not rs.connects == []:
  463.         raise AssertionError
  464.     if not not (c1.closed):
  465.         raise AssertionError
  466.     c1.flushed = False
  467.     if not not ch.is_flushed():
  468.         raise AssertionError
  469.  
  470.  
  471. def test_wrong_header_length():
  472.     c = DummyConnecter()
  473.     rs = DummyRawServer()
  474.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  475.     c1 = DummyRawConnection()
  476.     e.external_connection_made(c1)
  477.     if not c1.pop() == '':
  478.         raise AssertionError
  479.     if not c.log == []:
  480.         raise AssertionError
  481.     if not rs.connects == []:
  482.         raise AssertionError
  483.     if not not (c1.closed):
  484.         raise AssertionError
  485.     e.data_came_in(c1, chr(5) * 30)
  486.     if not c.log == []:
  487.         raise AssertionError
  488.     if not c1.closed:
  489.         raise AssertionError
  490.  
  491.  
  492. def test_wrong_header():
  493.     c = DummyConnecter()
  494.     rs = DummyRawServer()
  495.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  496.     c1 = DummyRawConnection()
  497.     e.external_connection_made(c1)
  498.     if not c1.pop() == '':
  499.         raise AssertionError
  500.     if not c.log == []:
  501.         raise AssertionError
  502.     if not rs.connects == []:
  503.         raise AssertionError
  504.     if not not (c1.closed):
  505.         raise AssertionError
  506.     e.data_came_in(c1, chr(len(protocol_name)) + 'a' * len(protocol_name))
  507.     if not c.log == []:
  508.         raise AssertionError
  509.     if not c1.closed:
  510.         raise AssertionError
  511.  
  512.  
  513. def test_wrong_download_id():
  514.     c = DummyConnecter()
  515.     rs = DummyRawServer()
  516.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  517.     c1 = DummyRawConnection()
  518.     e.external_connection_made(c1)
  519.     if not c1.pop() == '':
  520.         raise AssertionError
  521.     if not c.log == []:
  522.         raise AssertionError
  523.     if not rs.connects == []:
  524.         raise AssertionError
  525.     if not not (c1.closed):
  526.         raise AssertionError
  527.     e.data_came_in(c1, chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'e' * 20)
  528.     if not c1.pop() == '':
  529.         raise AssertionError
  530.     if not c.log == []:
  531.         raise AssertionError
  532.     if not c1.closed:
  533.         raise AssertionError
  534.  
  535.  
  536. def test_wrong_other_id():
  537.     c = DummyConnecter()
  538.     rs = DummyRawServer()
  539.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  540.     e.start_connection('dns', 'o' * 20)
  541.     if not c.log == []:
  542.         raise AssertionError
  543.     if not len(rs.connects) == 1:
  544.         raise AssertionError
  545.     if not rs.connects[0][0] == 'dns':
  546.         raise AssertionError
  547.     c1 = rs.connects[0][1]
  548.     del rs.connects[:]
  549.     if not c1.pop() == chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'a' * 20:
  550.         raise AssertionError
  551.     if not not (c1.closed):
  552.         raise AssertionError
  553.     e.data_came_in(c1, chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'b' * 20)
  554.     if not c.log == []:
  555.         raise AssertionError
  556.     if not c1.closed:
  557.         raise AssertionError
  558.  
  559.  
  560. def test_over_max_len():
  561.     c = DummyConnecter()
  562.     rs = DummyRawServer()
  563.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  564.     c1 = DummyRawConnection()
  565.     e.external_connection_made(c1)
  566.     if not c1.pop() == '':
  567.         raise AssertionError
  568.     if not c.log == []:
  569.         raise AssertionError
  570.     if not rs.connects == []:
  571.         raise AssertionError
  572.     if not not (c1.closed):
  573.         raise AssertionError
  574.     e.data_came_in(c1, chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'o' * 20)
  575.     if not c1.pop() == chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'a' * 20:
  576.         raise AssertionError
  577.     if not len(c.log) == 1 and c.log[0][0] == 'made':
  578.         raise AssertionError
  579.     ch = c.log[0][1]
  580.     del c.log[:]
  581.     if not not (c1.closed):
  582.         raise AssertionError
  583.     e.data_came_in(c1, chr(1) + chr(0) * 3)
  584.     if not c.log == [
  585.         ('lost', ch)]:
  586.         raise AssertionError
  587.     if not c1.closed:
  588.         raise AssertionError
  589.  
  590.  
  591. def test_keepalive():
  592.     s = []
  593.     
  594.     def sched(interval, thing, s = s):
  595.         s.append((interval, thing))
  596.  
  597.     c = DummyConnecter()
  598.     rs = DummyRawServer()
  599.     e = Encoder(c, rs, 'a' * 20, 500, sched, 30, 'd' * 20)
  600.     if not len(s) == 1:
  601.         raise AssertionError
  602.     if not s[0][1] == 30:
  603.         raise AssertionError
  604.     kfunc = s[0][0]
  605.     del s[:]
  606.     c1 = DummyRawConnection()
  607.     e.external_connection_made(c1)
  608.     if not c1.pop() == '':
  609.         raise AssertionError
  610.     if not c.log == []:
  611.         raise AssertionError
  612.     if not not (c1.closed):
  613.         raise AssertionError
  614.     kfunc()
  615.     if not c1.pop() == '':
  616.         raise AssertionError
  617.     if not c.log == []:
  618.         raise AssertionError
  619.     if not not (c1.closed):
  620.         raise AssertionError
  621.     if not s == [
  622.         (kfunc, 30)]:
  623.         raise AssertionError
  624.     del s[:]
  625.     e.data_came_in(c1, chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'o' * 20)
  626.     if not len(c.log) == 1 and c.log[0][0] == 'made':
  627.         raise AssertionError
  628.     del c.log[:]
  629.     if not c1.pop() == chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'a' * 20:
  630.         raise AssertionError
  631.     if not not (c1.closed):
  632.         raise AssertionError
  633.     kfunc()
  634.     if not c1.pop() == chr(0) * 4:
  635.         raise AssertionError
  636.     if not c.log == []:
  637.         raise AssertionError
  638.     if not not (c1.closed):
  639.         raise AssertionError
  640.  
  641.  
  642. def test_swallow_keepalive():
  643.     c = DummyConnecter()
  644.     rs = DummyRawServer()
  645.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  646.     c1 = DummyRawConnection()
  647.     e.external_connection_made(c1)
  648.     if not c1.pop() == '':
  649.         raise AssertionError
  650.     if not c.log == []:
  651.         raise AssertionError
  652.     if not rs.connects == []:
  653.         raise AssertionError
  654.     if not not (c1.closed):
  655.         raise AssertionError
  656.     e.data_came_in(c1, chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'o' * 20)
  657.     if not c1.pop() == chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'a' * 20:
  658.         raise AssertionError
  659.     if not len(c.log) == 1 and c.log[0][0] == 'made':
  660.         raise AssertionError
  661.     del c.log[:]
  662.     if not not (c1.closed):
  663.         raise AssertionError
  664.     e.data_came_in(c1, chr(0) * 4)
  665.     if not c.log == []:
  666.         raise AssertionError
  667.     if not not (c1.closed):
  668.         raise AssertionError
  669.  
  670.  
  671. def test_local_close():
  672.     c = DummyConnecter()
  673.     rs = DummyRawServer()
  674.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  675.     c1 = DummyRawConnection()
  676.     e.external_connection_made(c1)
  677.     if not c1.pop() == '':
  678.         raise AssertionError
  679.     if not c.log == []:
  680.         raise AssertionError
  681.     if not rs.connects == []:
  682.         raise AssertionError
  683.     if not not (c1.closed):
  684.         raise AssertionError
  685.     e.data_came_in(c1, chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'o' * 20)
  686.     if not c1.pop() == chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'a' * 20:
  687.         raise AssertionError
  688.     if not len(c.log) == 1 and c.log[0][0] == 'made':
  689.         raise AssertionError
  690.     ch = c.log[0][1]
  691.     del c.log[:]
  692.     if not not (c1.closed):
  693.         raise AssertionError
  694.     ch.close()
  695.     if not c.log == [
  696.         ('lost', ch)]:
  697.         raise AssertionError
  698.     del c.log[:]
  699.     if not c1.closed:
  700.         raise AssertionError
  701.  
  702.  
  703. def test_local_close_in_message_receive():
  704.     c = DummyConnecter()
  705.     rs = DummyRawServer()
  706.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  707.     c1 = DummyRawConnection()
  708.     e.external_connection_made(c1)
  709.     if not c1.pop() == '':
  710.         raise AssertionError
  711.     if not c.log == []:
  712.         raise AssertionError
  713.     if not rs.connects == []:
  714.         raise AssertionError
  715.     if not not (c1.closed):
  716.         raise AssertionError
  717.     e.data_came_in(c1, chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'o' * 20)
  718.     if not c1.pop() == chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'a' * 20:
  719.         raise AssertionError
  720.     if not len(c.log) == 1 and c.log[0][0] == 'made':
  721.         raise AssertionError
  722.     ch = c.log[0][1]
  723.     del c.log[:]
  724.     if not not (c1.closed):
  725.         raise AssertionError
  726.     c.close_next = True
  727.     e.data_came_in(c1, chr(0) * 3 + chr(4) + 'abcd')
  728.     if not c.log == [
  729.         ('got', ch, 'abcd'),
  730.         ('lost', ch)]:
  731.         raise AssertionError
  732.     if not c1.closed:
  733.         raise AssertionError
  734.  
  735.  
  736. def test_remote_close():
  737.     c = DummyConnecter()
  738.     rs = DummyRawServer()
  739.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  740.     c1 = DummyRawConnection()
  741.     e.external_connection_made(c1)
  742.     if not c1.pop() == '':
  743.         raise AssertionError
  744.     if not c.log == []:
  745.         raise AssertionError
  746.     if not rs.connects == []:
  747.         raise AssertionError
  748.     if not not (c1.closed):
  749.         raise AssertionError
  750.     e.data_came_in(c1, chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'o' * 20)
  751.     if not c1.pop() == chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'a' * 20:
  752.         raise AssertionError
  753.     if not len(c.log) == 1 and c.log[0][0] == 'made':
  754.         raise AssertionError
  755.     ch = c.log[0][1]
  756.     del c.log[:]
  757.     if not not (c1.closed):
  758.         raise AssertionError
  759.     e.connection_lost(c1)
  760.     if not c.log == [
  761.         ('lost', ch)]:
  762.         raise AssertionError
  763.     if not not (c1.closed):
  764.         raise AssertionError
  765.  
  766.  
  767. def test_partial_data_in():
  768.     c = DummyConnecter()
  769.     rs = DummyRawServer()
  770.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  771.     c1 = DummyRawConnection()
  772.     e.external_connection_made(c1)
  773.     if not c1.pop() == '':
  774.         raise AssertionError
  775.     if not c.log == []:
  776.         raise AssertionError
  777.     if not rs.connects == []:
  778.         raise AssertionError
  779.     if not not (c1.closed):
  780.         raise AssertionError
  781.     e.data_came_in(c1, chr(len(protocol_name)) + protocol_name + chr(0) * 4)
  782.     e.data_came_in(c1, chr(0) * 4 + 'd' * 20 + 'c' * 10)
  783.     e.data_came_in(c1, 'c' * 10)
  784.     if not c1.pop() == chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'a' * 20:
  785.         raise AssertionError
  786.     if not len(c.log) == 1 and c.log[0][0] == 'made':
  787.         raise AssertionError
  788.     del c.log[:]
  789.     if not not (c1.closed):
  790.         raise AssertionError
  791.  
  792.  
  793. def test_ignore_connect_of_extant():
  794.     c = DummyConnecter()
  795.     rs = DummyRawServer()
  796.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  797.     c1 = DummyRawConnection()
  798.     e.external_connection_made(c1)
  799.     if not c1.pop() == '':
  800.         raise AssertionError
  801.     if not c.log == []:
  802.         raise AssertionError
  803.     if not rs.connects == []:
  804.         raise AssertionError
  805.     if not not (c1.closed):
  806.         raise AssertionError
  807.     e.data_came_in(c1, chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'o' * 20)
  808.     if not c1.pop() == chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'a' * 20:
  809.         raise AssertionError
  810.     if not len(c.log) == 1 and c.log[0][0] == 'made':
  811.         raise AssertionError
  812.     del c.log[:]
  813.     if not not (c1.closed):
  814.         raise AssertionError
  815.     e.start_connection('dns', 'o' * 20)
  816.     if not c.log == []:
  817.         raise AssertionError
  818.     if not rs.connects == []:
  819.         raise AssertionError
  820.     if not not (c1.closed):
  821.         raise AssertionError
  822.  
  823.  
  824. def test_ignore_connect_to_self():
  825.     c = DummyConnecter()
  826.     rs = DummyRawServer()
  827.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  828.     c1 = DummyRawConnection()
  829.     e.start_connection('dns', 'a' * 20)
  830.     if not c.log == []:
  831.         raise AssertionError
  832.     if not rs.connects == []:
  833.         raise AssertionError
  834.     if not not (c1.closed):
  835.         raise AssertionError
  836.  
  837.  
  838. def test_conversion():
  839.     if not toint(tobinary(50000)) == 50000:
  840.         raise AssertionError
  841.  
  842.